package com.elderlyCare.controller.admin;


import cn.hutool.json.JSONUtil;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.elderlyCare.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

@RestController
@Slf4j
public class duiHua {

    //    @Value("${api-key}") // 注入配置文件中的 api-key
    private String apiKey = "sk-c218c98ee4094f80acd346aed0ad1186"; // 将静态变量改为实例变量
    private Session session;
    private ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    private ScheduledFuture<?> scheduledFuture;

    private int count = 0;


    // 前端向后端发送消息
    @RequestMapping("/admin/modelChat")
    public Result<String> onMessage(@RequestBody String message1) throws NoApiKeyException, InputRequiredException {

        log.info("【websocket消息】收到客户端发来的消息:{}", message1);
        if (count == 0){
            messages.add(createMessage(Role.SYSTEM, "我是一个宇航员,我会问你一些问题,你用中文回答,你的回答尽量的短一些不要超过100字."));
            count = 1;
        }

        messages.add(createMessage(Role.USER, message1));//将用户消息以用户身份加入消息列表
        GenerationParam param = createGenerationParam(messages);//创建提交参数
        GenerationResult result = callGenerationWithMessages(param);//获得返回结果集合
        String res = result.getOutput().getChoices().get(0).getMessage().getContent();
        log.info("模型输出:{}", res);
        messages.add(result.getOutput().getChoices().get(0).getMessage());//将模型输出以模型身份加入消息列表
        return Result.success(res);
    }

    //下面是千闻模型的代码

    List<Message> messages = new ArrayList<>();

    public GenerationParam createGenerationParam(List<Message> messages) {
        return GenerationParam.builder()
                .apiKey(apiKey)
                .model("qwen-max")
                .messages(messages)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .topP(0.8)
                .build();
    }

    public GenerationResult callGenerationWithMessages(GenerationParam param) throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        return gen.call(param);
    }

    private Message createMessage(Role role, String content) {
        return Message.builder().role(role.getValue()).content(content).build();
    }

}
